﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Description;
using PeaceBlogData;
using PeaceBlogData.Tables;

namespace PeaceBlog.Controllers.API
{
    public class AuthorsController : ApiController
    {
        private PeaceBlogEntity db = new PeaceBlogEntity();

        // GET: api/Authors
        [HttpGet]
        public async Task<List<Authors>> GetAuthors()
        {
            return await db.GetAllAuthorsAsync();
        }

        // GET: api/Authors/5
        [HttpGet]
        public async Task<IHttpActionResult> GetAuthors(int id)
        {
            var authors = await db.GetAuthor(id);
            if (authors == null)
            {
                return NotFound();
            }

            return Ok(authors);
        }
        
        // GET: api/Authors/baris-ceviz
        [HttpGet]
        public async Task<Authors> GetAuthors(String id)
        {
            var authors = await db.GetAuthor(id);
            if (authors == null)
            {
                return null;
            }
            return authors;
        }

        // PUT: api/Authors/5
        [HttpPut]
        public async Task<IHttpActionResult> UpdateAuthors(Authors authors)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            try
            {
                await db.UpdateAuthor(authors);
            }
            catch (DbUpdateConcurrencyException)
            {
                return NotFound();
            }

            return StatusCode(HttpStatusCode.NoContent);
        }

        // POST: api/Authors
        [HttpPost]
        public async Task<IHttpActionResult> AddAuthors(Authors authors)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            try
            {
                await db.AddAuthor(authors);
            }
            catch
            {
                return NotFound();
            }

            return Ok();
        }

        // DELETE: api/Authors/5
        [HttpDelete]
        public async Task<IHttpActionResult> DeleteAuthors(int id)
        {
            try
            {
                await db.DeleteAuthor(id);
            }
            catch
            {
                return NotFound();
            }
            return Ok();
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}